<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
        class="page-nav-bar"
        title="个人信息"
        left-arrow @click-left="$router.back()"/>
    <input type="file" ref="iptFile" @change="onChange" hidden>
    <!-- /导航栏 -->
    <van-cell @click="$refs.iptFile.click()" title="头像" is-link>
      <van-image
          class="avatar"
          fit="cover"
          round
          :src="user.photo"
      />
    </van-cell>
    <van-cell title="昵称" @click="isShowEditNickNamePopUp=true" :value="user.name" is-link/>
    <van-cell title="性别" @click="isShowEditGenderPopUp=true" :value="user.gender?'女':'男'" is-link/>
    <van-cell title="生日" @click="isShowEditBirthdayPopUp=true" :value="user.birthday" is-link/>
    <van-popup v-model="isShowEditNickNamePopUp" position="bottom" :style="{ height: '100%' }">
      <EditNickName v-if="isShowEditNickNamePopUp"
                    v-model="user.name"
                    @clickLeft="isShowEditNickNamePopUp=false"></EditNickName>
    </van-popup>
    <van-popup v-model="isShowEditGenderPopUp" position="bottom">
      <EditGender v-if="isShowEditGenderPopUp" v-model="user.gender"
                  @onCancel="isShowEditGenderPopUp=false"></EditGender>
    </van-popup>
    <van-popup v-model="isShowEditBirthdayPopUp" position="bottom">
      <EditBirthday v-model="user.birthday" @cancel="isShowEditBirthdayPopUp=false"></EditBirthday>
    </van-popup>
    <van-popup style="height: 100%;" v-model="isShowEditAvatarPopup" position="bottom">
      <EditAvatar v-if="isShowEditAvatarPopup" @updateAuthor="user.photo=$event" :img-src="imgSrc"
                  @cancel="isShowEditAvatarPopup=false"></EditAvatar>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/user'
import EditNickName from '@/views/User/components/EditNickName'
import EditGender from '@/views/User/components/EditGender'
import { Toast } from 'vant'
import EditBirthday from '@/views/User/components/EditBirthday'
import EditAvatar from '@/views/User/components/EditAvatar'

export default {
  name: 'user',
  components: {
    EditNickName,
    EditGender,
    EditBirthday,
    EditAvatar

  },
  data () {
    return {
      user: {},
      imgSrc: '',
      isShowEditNickNamePopUp: false, // 昵称弹出层
      isShowEditGenderPopUp: false,
      isShowEditBirthdayPopUp: false,
      isShowEditAvatarPopup: false
    }
  },
  created () {
    this.getUserInfo()
  },
  methods: {
    onChange (e) {
      // if (this.$refs.iptFile.value === '') return
      this.isShowEditAvatarPopup = true
      this.imgSrc = URL.createObjectURL(e.target.files[0])
      e.target.value = ''
    },
    async getUserInfo () {
      try {
        const res = await getUserProfile()
        this.user = res
        console.log(res)
      } catch (e) {
        Toast('网络错误')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
}
</style>
